﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;

using WHC.Hotel.UI.Controls;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmTeaCheckOut : BaseForm
    {
        public TeaSuiteInfo RoomInfo = null;

        private decimal amountMoney = 0.0M;
        private decimal offerMoney = 0.0M;
        private TeaOrderInfo orderInfo = null;

        public FrmTeaCheckOut()
        {
            InitializeComponent();
        }

        private void FrmRoomCheckOut_Load(object sender, EventArgs e)
        {            
            if (RoomInfo != null)
            {
                ShowTeaSuiteTreeView();
                BindData(RoomInfo);
            }
        }

        private void BindData(TeaSuiteInfo info)
        {
            if (info != null)
            {
                this.groupConsumeList.Text = string.Format("{0} {1}", info.RoomNo, "消费清单");

                amountMoney = OnShowStatus(info);
                this.lblAllMoney.Text = string.Format("消费总金额：{0:C2}", amountMoney);

                this.lblRoomNo.Text = info.RoomNo;
                this.lblRoomType.Text = info.RoomType;

                orderInfo = BLLFactory<TeaOrder>.Instance.GetCurrentOrder(info.RoomNo);
                if (orderInfo != null)
                {
                    this.lblAmount.Text = amountMoney.ToString("C2");
                    this.lblAmount.Tag = amountMoney;
                    this.lblConsume.Text = amountMoney.ToString("C2");
                    this.lblConsume.Tag = amountMoney;
                    this.lblYouhui.Text = offerMoney.ToString("C2");
                    this.lblYouhui.Tag = offerMoney;
                    this.lblDeposit.Text = orderInfo.DepositMoney.ToString("C2");
                    this.lblDeposit.Tag = orderInfo.DepositMoney;
                    this.txtAmount.Text = amountMoney.ToString("f2");
                    this.txtAmount.Tag = amountMoney;
                    
                    TimeSpan ts = DateTime.Now.Subtract(orderInfo.CreateDate);
                    int hour = ts.Hours + ts.Days * 24;
                    this.lblTime.Text = string.Format("{0}小时{1}分", hour, ts.Minutes);

                    this.txtPay.Text = "0.00";
                    txtPay_TextChanged(null, null);
                    this.txtPay.Focus();

                    if (!string.IsNullOrEmpty(orderInfo.OrderNo))
                    {
                        this.lblCheckHandNo.Text = orderInfo.OrderNo;
                    }
                    else
                    {
                        this.lblCheckHandNo.Text = "无";
                    }

                    this.lblCheckOutNo.Text = BLLFactory<TeaSuiteBill>.Instance.GetRoomBillNumber();//账单编号
                }
            }
        }

        private void ShowTeaSuiteTreeView()
        {
            #region 项目类型信息明细类别

            this.treeView1.Nodes.Clear();
            this.treeView1.BeginUpdate();

            List<TeaSuiteTypeInfo> typeList = BLLFactory<TeaSuiteType>.Instance.GetAll();
            foreach (TeaSuiteTypeInfo typeInfo in typeList)
            {
                string nodeName = string.Format("{0}  {1}", typeInfo.Name, typeInfo.Price);
                TreeNode typeNode = new TreeNode(nodeName, 0, 0);
                this.treeView1.Nodes.Add(typeNode);
                //typeNode.Tag = info;

                List<TeaSuiteInfo> roomList = BLLFactory<TeaSuite>.Instance.FindRooms(typeInfo.Name, TeaSuiteStatus.占用.ToString());
                foreach (TeaSuiteInfo roomInfo in roomList)
                {
                    TreeNode subNode = new TreeNode(roomInfo.RoomNo, 1, 1);
                    subNode.Tag = roomInfo;
                    typeNode.Nodes.Add(subNode);
                }
            }
            this.treeView1.EndUpdate();
            this.treeView1.ExpandAll();

            #endregion
        }
        
        private decimal OnShowStatus(TeaSuiteInfo roomInfo)
        {
            decimal allMoney = 0.0M;

            #region 更新消费记录
            if (roomInfo != null)
            {
                List<TeaConsumerListInfo> consumerList = BLLFactory<TeaSuite>.Instance.GetAllConsumption(roomInfo.RoomNo);

                this.lvwDetail.Items.Clear();
                int i = 1;
                offerMoney = 0.0M;
                foreach (TeaConsumerListInfo info in consumerList)
                {
                    ListViewItem item = new ListViewItem(info.RoomNo);
                    string itemName = info.ItemName;
                    if (info.ItemName.Contains("房间"))
                    {
                        if (info.ItemName.Contains("房间买断消费"))
                        {
                            itemName = info.ItemName + "(买断计费)";
                        }
                        else
                        {
                            itemName = info.ItemName + "(计时计费)";
                        }
                    }
                    item.SubItems.Add(itemName);
                    //item.SubItems.Add(info.ItemName + (info.ItemName.Contains("房间买断消费") ? " (买断计费)" : " (计时计费)"));
                    
                    item.SubItems.Add(info.Price.ToString("C2"));
                    item.SubItems.Add(info.Discount.ToString());
                    item.SubItems.Add(info.DiscountPrice.ToString("C2"));
                    offerMoney += info.Price - info.DiscountPrice;//优惠金额
                    item.SubItems.Add((info.Price - info.DiscountPrice).ToString("C2"));
                    item.SubItems.Add(info.Quantity.ToString());
                    item.SubItems.Add(info.Amount.ToString("C2"));
                    item.SubItems.Add(info.BeginTime.ToString());
                    item.SubItems.Add(info.Creator);
                    item.Tag = info;
                    if (info.Quantity < 0)
                    {
                        item.ForeColor = Color.Red;
                    }

                    this.lvwDetail.Items.Add(item);
                    allMoney += info.Amount;
                    i++;
                }
            }
            #endregion

            return allMoney;
        }
        
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode selectedNode = e.Node;
            if (selectedNode.Tag != null)
            {
                TeaSuiteInfo info = selectedNode.Tag as TeaSuiteInfo;
                if (info != null)
                {
                    RoomInfo = info;//重新更新变量，方便结算
                    BindData(info);
                }
            }
        }

        private void txtPay_TextChanged(object sender, EventArgs e)
        {
            string payInput = "0";
            if (txtPay.Text.Trim('-').Length > 0)
            {
                payInput = txtPay.Text;
            }

            decimal payMoney = Convert.ToDecimal(payInput);
            decimal amount = Convert.ToDecimal(this.txtAmount.Text); 
            decimal deposit = GetDeposit();
            this.lblChange.Text = (payMoney + deposit - amount).ToString("f2");

        }

        private string GetPayType()
        {
            string result = "已结账";
            if (this.chkSign.Checked)
            {
                result = chkSign.Text;
            }
            else if (this.chkHoldup.Checked)
            {
                result = chkHoldup.Text;
            }
            else if (this.chkFree.Checked)
            {
                result = chkFree.Text;
            }
            else if (this.chkBack.Checked)
            {
                result = chkBack.Text;
            }

            return result;
        }

        private decimal GetDeposit()
        {
            decimal deposit = Convert.ToDecimal(this.lblDeposit.Tag);
            if (this.chkSign.Checked || this.chkHoldup.Checked || this.chkFree.Checked)
            {
                if (this.chkDeposit.Checked)
                {
                    deposit = 0M;//退押金
                }
            }
            return deposit;
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageUtil.ShowYesNoAndTips("您是否确定结账") != DialogResult.Yes)
                    return;

                #region 构造账单信息

                if (this.chkSign.Checked && this.txtName.Text.Trim() == "")
                {
                    MessageUtil.ShowTips("签单需要指定客户，请选择客户");
                    this.txtName.Focus();
                    return;
                }

                decimal amount = Convert.ToDecimal(this.txtAmount.Text);
                decimal payMoney = Convert.ToDecimal(this.txtPay.Text);
                decimal deposit = GetDeposit();
                if ((payMoney+deposit) < amount)
                {
                    MessageUtil.ShowTips("付款金额不足，不能进行结账");
                    this.txtPay.Focus();
                    return;
                }

                TeaSuiteBillInfo billInfo = new TeaSuiteBillInfo();
                billInfo.BillNumber = this.lblCheckOutNo.Text;
                billInfo.Cash = amount;
                billInfo.CheckStatus = GetPayType();//"已结账";
                billInfo.Consumption = Convert.ToDecimal(this.lblConsume.Tag);
                billInfo.CreateDate = DateTime.Now;
                billInfo.Creator = Portal.gc.LoginInfo.FullName;
                //billInfo.CustomerName = this.lblName.Text;
                billInfo.Deposit = GetDeposit();
                billInfo.InHotelDate = orderInfo.CreateDate;//入店时间
                billInfo.Invoice = this.chkFP.Checked;
                billInfo.KeepChange = Convert.ToDecimal(this.lblChange.Text);
                billInfo.Note = this.txtNote.Text;
                billInfo.OfferMoney = Convert.ToDecimal(this.lblYouhui.Tag);
                billInfo.PayMoney = Convert.ToDecimal(this.txtAmount.Text);
                billInfo.PayType = "现金";
                billInfo.RoomNo = this.lblRoomNo.Text;
                billInfo.CustomerName = this.txtName.Text;//签单人

                #endregion

                if (billInfo.CheckStatus == CheckStatus.退单.ToString())
                {
                    //退单的时候，把消费的商品退回去（增加退货记录）
                    //同时还原库存
                    List<TeaConsumerListInfo> consumerList = BLLFactory<TeaSuite>.Instance.GetGoodsConsumption(RoomInfo.RoomNo);
                    foreach (TeaConsumerListInfo consumerInfo in consumerList)
                    {
                        int originalQuantity = Convert.ToInt32(consumerInfo.Quantity);
                        consumerInfo.Amount = -consumerInfo.Amount;
                        consumerInfo.Quantity = -consumerInfo.Quantity;
                        bool tempFlag = BLLFactory<TeaConsumerList>.Instance.Insert(consumerInfo);
                        if (tempFlag)
                        {
                            BLLFactory<TeaGoods>.Instance.AddStockQuantiy(consumerInfo.ItemNo, consumerInfo.ItemName, originalQuantity);
                        }
                    }
                }
                else
                {
                    //结帐前，需要写入计算好的房间消费清单到数据库中
                    //（因为结账前的房间费用都是动态计算的，没有写入数据库中）
                    List<TeaConsumerListInfo> consumerList = BLLFactory<TeaSuite>.Instance.GetConsumption(RoomInfo.RoomNo);
                    foreach (TeaConsumerListInfo consumerInfo in consumerList)
                    {
                        BLLFactory<TeaConsumerList>.Instance.Insert(consumerInfo);
                    }
                }

                bool success = BLLFactory<TeaSuiteBill>.Instance.CheckOutRoom(billInfo, orderInfo);
                if (!success)
                {
                    MessageUtil.ShowWarning("房间结账失败");
                    return;
                }

                #region 增加收银明细
                string note = string.Format("[结账时{0}退押金]", this.chkDeposit.Checked ? "已" : "未");
                decimal money = (amountMoney - orderInfo.DepositMoney);//默认该次收取金额=实收-押金
                if (this.chkDeposit.Checked)
                {
                    money = -1 * Convert.ToDecimal(this.lblDeposit.Tag);//退押金
                }

                if (this.chkFree.Checked)
                {
                    note += this.chkFree.Text;
                }
                else if (this.chkHoldup.Checked)
                {
                    note += this.chkHoldup.Text;
                }
                else if (this.chkBack.Checked)
                {
                    note += this.chkBack.Text;
                }
                else if (this.chkSign.Checked)
                {
                    note += this.chkSign.Text;
                }
                else
                {
                    note += "已结账";
                }
                note += string.Format(" 结账单号:{0} {1}", billInfo.BillNumber, this.txtNote.Text);
                AddMoneyDetail(money, billInfo.PayType, note);
                #endregion


                this.DialogResult = DialogResult.OK;//成功
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                MessageUtil.ShowError(ex.Message);
            }
        }

        /// <summary>
        /// 增加收银记录
        /// </summary>
        /// <returns></returns>
        private void AddMoneyDetail(decimal payMoney, string payType, string note)
        {
            if (payMoney != 0M)
            {
                TeaMoneyDetailInfo info = new TeaMoneyDetailInfo();
                info.Cash = payMoney;
                info.CashierType = "结账收银";
                info.Creator = Portal.gc.LoginInfo.FullName;
                info.PayDate = DateTime.Now;
                info.Customer_ID = 0;
                info.CustomerName = orderInfo.CustomerName;
                info.RoomNo = orderInfo.RoomNo;
                info.PayType = payType;
                info.Note = note;

                BLLFactory<TeaMoneyDetail>.Instance.Insert(info);
            }
        }

        private void btnUnitedCheck_Click(object sender, EventArgs e)
        {
            FrmTeaUnitedCheckOut dlg = new FrmTeaUnitedCheckOut();
            dlg.orderInfo = orderInfo;
            dlg.lblAmount.Text = Convert.ToDecimal(this.lblAmount.Tag).ToString("f2");
            dlg.lblDeposit.Text = orderInfo.DepositMoney.ToString("f2");

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    #region 构造账单信息

                    decimal amount = Convert.ToDecimal(this.txtAmount.Text);
                    decimal payMoney = Convert.ToDecimal(this.txtPay.Text);

                    TeaSuiteBillInfo billInfo = new TeaSuiteBillInfo();
                    billInfo.BillNumber = this.lblCheckOutNo.Text;
                    //支付信息
                    billInfo.Cash = Convert.ToDecimal(dlg.txtCashPay.Text);
                    billInfo.Card = Convert.ToDecimal(dlg.txtCard.Text);
                    billInfo.CardNumber = dlg.txtCardNumber.Text;
                    billInfo.NationalCredit = Convert.ToDecimal(dlg.txtNationalCredit.Text);
                    billInfo.CreditCardNumber = dlg.txtNationalCreditCard.Text;
                    billInfo.InterCredit = Convert.ToDecimal(dlg.txtInterCredit.Text);
                    billInfo.InterCreditCardNumber = dlg.txtInterCreditCard.Text;
                    billInfo.DaijinTicket = Convert.ToDecimal(dlg.txtDaijinTicket.Text);
                    billInfo.KeepChange = Convert.ToDecimal(dlg.lblChange.Text);
                    billInfo.Deposit = Convert.ToDecimal(dlg.lblDeposit.Text);
                    billInfo.Note = dlg.txtNote.Text;

                    billInfo.CheckStatus = "已结账";
                    billInfo.Consumption = Convert.ToDecimal(this.lblConsume.Tag);
                    billInfo.CreateDate = DateTime.Now;
                    billInfo.Creator = Portal.gc.LoginInfo.FullName;
                    //billInfo.CustomerName = this.lblName.Text;
                    billInfo.InHotelDate = orderInfo.CreateDate;//入店时间
                    billInfo.Invoice = this.chkFP.Checked;
                    billInfo.OfferMoney = Convert.ToDecimal(this.lblYouhui.Tag);
                    billInfo.PayMoney = Convert.ToDecimal(this.txtAmount.Text);
                    billInfo.PayType = "联合";
                    billInfo.RoomNo = this.lblRoomNo.Text;

                    #endregion

                    bool success = BLLFactory<TeaSuiteBill>.Instance.CheckOutRoom(billInfo, orderInfo);
                    if (!success)
                    {
                        MessageUtil.ShowWarning("房间结账失败");
                        return;
                    }

                    #region 添加收银明细
                    string note = string.Format("[联合支付时退押金]结账单号:{0}", billInfo.BillNumber);
                    AddMoneyDetail(-orderInfo.DepositMoney, "现金", note);

                    TeaMoneyDetailInfo info = new TeaMoneyDetailInfo();
                    info.Cash = billInfo.Cash;
                    info.Card = billInfo.Card;
                    info.DaijinTicket = billInfo.DaijinTicket;
                    info.InterCredit = billInfo.InterCredit;
                    info.NationalCredit = billInfo.NationalCredit;
                    info.CashierType = "结账收银";
                    info.Creator = Portal.gc.LoginInfo.FullName;
                    info.PayDate = DateTime.Now;
                    info.Customer_ID = 0;
                    info.CustomerName = orderInfo.CustomerName;
                    info.RoomNo = orderInfo.RoomNo;
                    info.PayType = billInfo.PayType;
                    info.Note = dlg.txtNote.Text;
                    BLLFactory<TeaMoneyDetail>.Instance.Insert(info);
                    #endregion

                    this.DialogResult = DialogResult.OK;//成功
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
            }
        }

        private void btnPrintDetail_Click(object sender, EventArgs e)
        {
            FrmPrintOptions dlg = new FrmPrintOptions();
            dlg.txtTitle.Text = string.Format("{0}\r\n茶室餐台({1})结账清单      账单号：{2}\r\n",
                Portal.gc.gAppUnit, RoomInfo.RoomNo, this.lblCheckOutNo.Text);
            decimal amount = Convert.ToDecimal(this.txtAmount.Text);//实收
            dlg.txtBottom.Text = string.Format("{0}           实收：{1}(大写：{2})      现金：{2}      找零{3}",
                lblAllMoney.Text, amount.ToString("C2"), RMBUtil.CmycurD(amount), Convert.ToDecimal(this.txtPay.Text).ToString("C2"),
                Convert.ToDecimal(this.lblChange.Text).ToString("C2"));
            dlg.txtBottom.Text += string.Format("\r\n签单单位：                          签单人：\t\r\n");
            dlg.listView = lvwDetail;
            dlg.ShowDialog();
        }

        private void ResetStatus()
        {
            this.chkDeposit.Checked = false;

            decimal payMoney = 0.00M;
            this.txtPay.Text = payMoney.ToString("f2");
            this.txtPay.Focus();

            decimal amount = Convert.ToDecimal(this.txtAmount.Text);
            this.txtPay.Text = amount.ToString("f2"); 
            this.txtNote.Text = "";
            txtPay_TextChanged(null, null);
        }

        private void chkSign_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSign.Checked)
            {
                this.chkBack.Checked = false;
                this.chkFree.Checked = false;
                this.chkHoldup.Checked = false;
                this.chkDeposit.Checked = false;

                decimal amount = Convert.ToDecimal(this.txtAmount.Text);
                this.txtPay.Text = amount.ToString("f2"); 
                txtPay_TextChanged(null, null);
                this.txtNote.Text = string.Format("{0}签单金额:{1}",
                    Portal.gc.LoginInfo.FullName, this.txtPay.Text);
            }
            else
            {
                ResetStatus();
            }
        }

        private void chkHoldup_CheckedChanged(object sender, EventArgs e)
        {
            if (chkHoldup.Checked)
            {
                this.chkBack.Checked = false;
                this.chkFree.Checked = false;
                this.chkSign.Checked = false;
                this.chkDeposit.Checked = true;

                this.txtPay.Text = "0.00";
                this.txtAmount.Text = "0.00";
                this.txtNote.Text = chkHoldup.Text;
                txtPay_TextChanged(null, null);
            }
            else
            {
                ResetStatus();
            }
        }

        private void chkFree_CheckedChanged(object sender, EventArgs e)
        {
            if (chkFree.Checked)
            {
                this.chkBack.Checked = false;
                this.chkSign.Checked = false;
                this.chkHoldup.Checked = false;
                this.chkDeposit.Checked = true;

                this.txtPay.Text = "0.00";
                this.txtAmount.Text = "0.00";
                this.lblChange.Text = "0.0";
                txtPay_TextChanged(null, null);
                this.txtNote.Text = chkFree.Text;
            }
            else
            {
                ResetStatus();
            }
        }

        private void chkBack_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBack.Checked)
            {
                this.chkSign.Checked = false;
                this.chkFree.Checked = false;
                this.chkHoldup.Checked = false;
                this.chkDeposit.Checked = true;

                this.txtPay.Text = "0.00";
                this.txtAmount.Text = "0.00";
                txtPay_TextChanged(null, null);
                this.txtNote.Text = chkBack.Text;
            }
            else
            {
                ResetStatus();
            }
        }

        private void btnPosPrint_Click(object sender, EventArgs e)
        {
            if (this.lvwDetail.Items.Count == 0)
            {
                MessageUtil.ShowTips("没有消费记录");
                return;
            }

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0}\r\n\r\n", Portal.gc.gAppUnit);
            sb.AppendFormat("茶室消费\r\n");
            sb.AppendFormat("结账单号：{0}\r\n", this.lblCheckOutNo.Text);
            sb.AppendFormat("客户姓名：{0}\r\n", this.txtName.Text);
            sb.AppendFormat("结账餐台：{0}\r\n", RoomInfo.RoomNo);
            sb.AppendFormat("打印时间：{0}\r\n\r\n", DateTime.Now);

            sb.AppendFormat("项目     单价  数量  金额\r\n");
            for (int i = 0; i < lvwDetail.Items.Count; i++)
            {
                ListViewItem item = lvwDetail.Items[i];
                string itemName = item.SubItems[1].Text;
                if (itemName.Contains("买断消费"))
                {
                    itemName = "买断消费";
                }
                sb.AppendFormat("{0}", itemName); 
                sb.AppendFormat(" {0}", item.SubItems[2].Text);
                sb.AppendFormat(" {0}", item.SubItems[6].Text);
                sb.AppendFormat(" {0}\r\n", item.SubItems[7].Text);
            }
            sb.AppendFormat("\r\n\r");
            sb.AppendFormat("总 金 额：{0}\r\n", Convert.ToDecimal(this.lblConsume.Tag).ToString("C2"));
            sb.AppendFormat("实收金额：{0}\r\n", Convert.ToDecimal(this.txtAmount.Text).ToString("C2"));
            sb.AppendFormat("实收大写：{0}\r\n", RMBUtil.CmycurD(this.txtAmount.Text));
            sb.AppendFormat("现    金：{0}\r\n", Convert.ToDecimal(this.txtPay.Text).ToString("C2"));
            sb.AppendFormat("找    零：{0}\r\n", Convert.ToDecimal(this.lblChange.Text).ToString("C2"));
            sb.AppendFormat("\r\n\r");

            Portal.gc.PosPrint(sb.ToString());
        }

        private void menu_Discount_Click(object sender, EventArgs e)
        {
            FrmDiscount dlg = new FrmDiscount();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                foreach (ListViewItem item in this.lvwDetail.SelectedItems)
                {
                    TeaConsumerListInfo info = item.Tag as TeaConsumerListInfo;
                    if (info != null)
                    {
                        //赠送的不再参与打折
                        if (info.ItemName.Contains("(赠)"))
                        {
                            continue;
                        }

                        double rate = Convert.ToDouble(dlg.txtDiscountRate.Text);
                        info.Discount = rate;
                        info.DiscountPrice = info.Price * Convert.ToDecimal(rate);
                        info.Amount = info.DiscountPrice * Convert.ToDecimal(info.Quantity);

                        BLLFactory<TeaConsumerList>.Instance.Update(info, info.ID.ToString());
                        if (info.ItemName == "房间费" && info.ID == 0)
                        {
                            RoomTempDiscountInfo tempInfo = BLLFactory<RoomTempDiscount>.Instance.GetDiscount("茶室", info.Order_ID, info.RoomNo);
                            if (tempInfo != null)
                            {
                                tempInfo.DiscountRate = rate;
                                BLLFactory<RoomTempDiscount>.Instance.Update(tempInfo, tempInfo.ID.ToString());
                            }
                            else
                            {
                                tempInfo = new RoomTempDiscountInfo();
                                tempInfo.DiscountRate = rate;
                                tempInfo.Order_ID = info.Order_ID;
                                tempInfo.OrderType = "茶室";
                                tempInfo.RoomNo = info.RoomNo;
                                tempInfo.CreateDate = DateTime.Now;
                                BLLFactory<RoomTempDiscount>.Instance.Insert(tempInfo);
                            }
                        }
                        else if (info.ItemName == "房间买断消费" && info.ID == 0)
                        {
                            RoomTempDiscountInfo tempInfo = BLLFactory<RoomTempDiscount>.Instance.GetDiscount("茶室买断", info.Order_ID, info.RoomNo);
                            if (tempInfo != null)
                            {
                                tempInfo.DiscountRate = rate;
                                BLLFactory<RoomTempDiscount>.Instance.Update(tempInfo, tempInfo.ID.ToString());
                            }
                            else
                            {
                                tempInfo = new RoomTempDiscountInfo();
                                tempInfo.DiscountRate = rate;
                                tempInfo.Order_ID = info.Order_ID;
                                tempInfo.OrderType = "茶室买断";
                                tempInfo.RoomNo = info.RoomNo;
                                tempInfo.CreateDate = DateTime.Now;
                                BLLFactory<RoomTempDiscount>.Instance.Insert(tempInfo);
                            }
                        }
                    }
                }

                BindData(RoomInfo);
            }
        }

        private void lvwDetail_DoubleClick(object sender, EventArgs e)
        {
            menu_Discount_Click(null, null);
        }

        private void txtAmount_TextChanged(object sender, EventArgs e)
        {
            txtPay_TextChanged(null, null);
        }

        private void ChangeCustomer(string customerID)
        {
            if (!string.IsNullOrEmpty(customerID))
            {
                CustomerInfo info = BLLFactory<Customer>.Instance.FindByID(customerID);
                if (info != null)
                {
                    this.txtName.Text = info.Name;
                }
            }
        }

        private void btnNameSearch_Click(object sender, EventArgs e)
        {
            FrmCustomerSearch dlg = new FrmCustomerSearch();
            dlg.txtName.Text = this.txtName.Text;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(dlg.SelectedID))
                {
                    ChangeCustomer(dlg.SelectedID);
                }
            }
        }
    }
}
